<template>
  <div>
    <Nav message="-1" />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      @add="onAdd"
      @edit="onEdit"
      @select="onSelect"
    />
  </div>
</template>
<script>
import Nav from "@/components/Nav.vue";
export default {
  components: {
    Nav,
  },
  created() {
    let isShopToken = this.$cookie.get("shoptoken");
    if (isShopToken == null) {
      this.$router.push({ path: "/login", query: { item: this.$route.path } });
    }

    let skuId = this.$route.query.skuId;
    let skuNum = this.$route.query.skuNum;
    this.detailGoAddressSkuId = skuId;
    this.detailGoAddressSkuNum = skuNum;

    const _this = this;
    axios
      .get(this.$api + "member/findBuyerAddressList", {
        params: {
          token: isShopToken,
        },
      })
      .then(function (resp) {
        _this.list = resp.data.data.list;

        //保留一份有&的地址List
        for (let index = 0; index < resp.data.data.list.length; index++) {
          let hasFlagAddressObject = {
            hasFlagId: null,
            hasFlagidAddress: null,
          };
          hasFlagAddressObject.hasFlagId = resp.data.data.list[index].id;
          hasFlagAddressObject.hasFlagidAddress =
            resp.data.data.list[index].address;
          _this.onEditHasFlagAddress[index] = hasFlagAddressObject;
        }
        console.log(_this.onEditHasFlagAddress);
      });
  },
  beforeUpdate() {
    //去掉详细地址中的&
    const _this = this;
    for (let index = 0; index < _this.list.length; index++) {
      let flagIndex = _this.list[index].address.indexOf("&");
      let substringAddressLast = _this.list[index].address.substring(
        flagIndex + 1
      );
      let substringAddressFirst = _this.list[index].address.substring(
        0,
        flagIndex
      );
      _this.list[index].address = substringAddressFirst + substringAddressLast;
    }
  },
  data() {
    return {
      chosenAddressId: "", //默认选中地址
      list: [
        // {
        //   id: "1",
        //   areaCode: "330106",
        //   name: "Anni",
        //   tel: "13000000000",
        //   address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        //   // isDefault: true,
        // },
        // {
        //   id: "2",
        //   areaCode: "330105",
        //   name: "Camille",
        //   tel: "13100000000",
        //   address: "浙江省杭州市拱墅市区莫干山路 50 号",
        // },
      ],
      onEditHasFlagAddress: [
        // {
        //   hasFlagId: "2",
        //   hasFlagidAddress: "浙江省杭州市拱墅市区莫干山路 50 号",
        // },
      ], //给修改页面发数据时，需要一个有&的地址
      detailGoAddressSkuId: null, //详情页跳地址
      detailGoAddressSkuNum: null, //详情页跳地址
      addressGoDetailAddressId: null, //地址页面去详情
    };
  },
  methods: {
    onAdd() {
      this.$router.push({
        path: "/addressNew",
        query: {
          skuId: this.detailGoAddressSkuId,
          skuNum: this.detailGoAddressSkuNum,
        },
      });
    },
    onEdit(item, index) {
      console.log("点击修改");
      console.log(item);

      //根据点击的 地址id 找出有标记的地址
      let hasFlagAddress;
      for (let index = 0; index < this.onEditHasFlagAddress.length; index++) {
        if (this.onEditHasFlagAddress[index].hasFlagId == item.id) {
          hasFlagAddress = this.onEditHasFlagAddress[index].hasFlagidAddress;
        }
      }

      var unicode = this.encodeUnicode(hasFlagAddress);
      this.$router.push({
        path: "/addressEdit",
        query: {
          id: item.id,
          address: unicode,
          areaCode: item.areaCode,
          name: item.name,
          tel: item.tel,
          skuId: this.detailGoAddressSkuId,
          skuNum: this.detailGoAddressSkuNum,
        },
      });
    },
    //跳详情页
    onSelect(item) {
      console.log(item);
      this.addressGoDetailAddressId = item.id;
      this.$router.push({
        path: "/orderDetail",
        query: {
          skuId: this.detailGoAddressSkuId,
          skuNum: this.detailGoAddressSkuNum,
          addressId: this.addressGoDetailAddressId,
        },
      });
    },

    // 转为unicode 编码
    encodeUnicode(str) {
      var res = [];
      for (var i = 0; i < str.length; i++) {
        res[i] = ("00" + str.charCodeAt(i).toString(16)).slice(-4);
      }
      return "\\u" + res.join("\\u");
    },
  },
};
</script>
<style scoped>
.van-button {
  background: rosybrown;
  border: rosybrown;
  font-weight: bold;
}
::v-deep .van-radio__icon {
  visibility: hidden;
  width: 0%;
}
</style>